﻿using System.Data;
using System.Data.Odbc;
using System.Data.OleDb;
using System.Data.SqlClient;
using MySql.Data.MySqlClient;

namespace org.se.fs.model
{
    public class DatabaseFactory
    {
        public static IDbConnection GetConnection(ProviderType providerType)
        {
            switch (providerType)
            {
                case ProviderType.SqlServer:
                    return new SqlConnection();

                case ProviderType.OleDb:
                    return new OleDbConnection();

                case ProviderType.Odbc:
                    return new OdbcConnection();

                case ProviderType.Oracle:
                    //iDbConnection = new OracleConnection();
                    break;
                case ProviderType.Mysql:
                    return new MySqlConnection();
            }
            return null;
        }

        public static IDbCommand GetCommand(ProviderType providerType)
        {
            switch (providerType)
            {
                case ProviderType.SqlServer:
                    return new SqlCommand();
                case ProviderType.OleDb:
                    return new OleDbCommand();
                case ProviderType.Odbc:
                    return new OdbcCommand();
                case ProviderType.Oracle:
                    //return new OracleCommand();
                case ProviderType.Mysql:
                    return new MySqlCommand();
                default:
                    return null;
            }
        }

        public static IDbDataAdapter GetDataAdapter(ProviderType providerType)
        {
            switch (providerType)
            {
                case ProviderType.SqlServer:
                    return new SqlDataAdapter();
                case ProviderType.OleDb:
                    return new OleDbDataAdapter();
                case ProviderType.Odbc:
                    return new OdbcDataAdapter();
                case ProviderType.Oracle:
                    //return new OracleDataAdapter();
                case ProviderType.Mysql:
                    return new MySqlDataAdapter();
                default:
                    return null;
            }
        }

        public static IDbTransaction GetTransaction(ProviderType providerType)
        {
            var iDbConnection = GetConnection(providerType);
            var iDbTransaction = iDbConnection.BeginTransaction();
            return iDbTransaction;
        }

        public static IDataParameter GetParameter(ProviderType providerType)
        {
            IDataParameter iDataParameter = null;
            switch (providerType)
            {
                case ProviderType.SqlServer:
                    iDataParameter = new SqlParameter();
                    break;
                case ProviderType.OleDb:
                    iDataParameter = new OleDbParameter();
                    break;
                case ProviderType.Odbc:
                    iDataParameter = new OdbcParameter();
                    break;
                case ProviderType.Oracle:
                    //iDataParameter = new OracleParameter();
                    break;
                case ProviderType.Mysql:
                    iDataParameter = new MySqlParameter();
                    break;

            }
            return iDataParameter;
        }

        public static IDbDataParameter[] GetParameters(ProviderType providerType, int paramsCount)
        {
            var idbParams = new IDbDataParameter[paramsCount];

            switch (providerType)
            {
                case ProviderType.SqlServer:
                    for (var i = 0; i < paramsCount; ++i)
                        idbParams[i] = new SqlParameter();
                    break;

                case ProviderType.OleDb:
                    for (var i = 0; i < paramsCount; ++i)
                        idbParams[i] = new OleDbParameter();
                    break;

                case ProviderType.Odbc:
                    for (var i = 0; i < paramsCount; ++i)
                        idbParams[i] = new OdbcParameter();
                    break;

                case ProviderType.Oracle:
                    //for (int i = 0; i <intParamsLength; ++i)
                    //{
                    //  idbParams[i] = newOracleParameter();
                    //}
                    break;

                case ProviderType.Mysql:
                    for (var i = 0; i < paramsCount; ++i)
                        idbParams[i] = new MySqlParameter();
                    break;

                default:
                    idbParams = null;
                    break;
            }
            return idbParams;
        }
    }
}